package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.ComUnitName;
import com.happyhome.constants.RecordStatus;
import com.happyhome.datamodel.House;
import com.happyhome.datamodel.QHouse;
import com.happyhome.datamodel.QRoom;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class HouseDaoImpl extends GenericDAOImpl<House, Long> implements HouseDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(HouseDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@Override
	public House getHouseByCode(String codeHouse) {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		
		query.from(house);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE)).and(house.codeHouse.eq(codeHouse));
		
		query.where(where);
		
		House houseResult = query.uniqueResult(house);
		return houseResult;
	}

	@Override
	public List<House> getListHousesInListId(List<Long> listHousesId) {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		
		query.from(house);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE))
			 .and(house.idHouse.in(listHousesId));
		
		query.where(where);
		
		List<House> resultHouses = query.list(house);
		return resultHouses;
	}

	@Override
	public List<House> getListHousesNotInListId(List<Long> listHousesId) {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		
		query.from(house);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE))
			 .and(house.idHouse.notIn(listHousesId));
		
		query.where(where);
		
		List<House> resultHouses = query.list(house);
		return resultHouses;
	}

	@Override
	public List<House> getListHousesIsDisplay() {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		
		query.from(house);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE))
			 .and(house.isDisplay.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<House> resultHouses = query.list(house);
		return resultHouses;
	}

	@Override
	public List<House> getListHousesNewestLimit(Long limit) {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		
		query.from(house);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE));
		query.where(where).orderBy(house.makeDate.desc()).limit(limit);
		
		List<House> resultHouses = query.list(house);
		return resultHouses;
	}

	@Override
	public String getGoogleMap(Long idRoom) {
		JPQLQuery query = new JPAQuery(em());
		QHouse house = QHouse.house;
		QRoom room = QRoom.room;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(room.status.eq(RecordStatus.TRUE)).and(room.idRoom.eq(idRoom));
		query.from(room).where(where).leftJoin(room.house, house);
		
		List<String> googleMap = query.list(house.googleMap);
		if(googleMap != null && googleMap.size() > 0){
			return googleMap.get(0);
		}
		return null;
	}
}
